//
//  ViewController.m
//  05-UIScrollView
//
//  Created by dllo on 16/7/20.
//  Copyright © 2016年 YMJ. All rights reserved.
//

#import "ViewController.h"
// k + 驼峰命名
#define kScreenBounds [UIScreen mainScreen].bounds
#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height
@interface ViewController ()
@property (nonatomic, strong) UIImageView *imageView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor brownColor];
    self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"person"]];
    self.imageView.center = CGPointMake(kWidth / 2, kHeight / 2);
    
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:kScreenBounds];
    scrollView.backgroundColor = [UIColor whiteColor];

    [self.view addSubview:scrollView];
    

    // 1.contentSize 默认CGSizeZero CGSizeMake(0,0)
    scrollView.contentSize = CGSizeMake(kWidth * 3, kHeight * 2);
    scrollView.contentOffset = CGPointMake(0, 0);
    // 2.contentInset 添加内边距(被遮盖底部文字等用到)
    scrollView.contentInset = UIEdgeInsetsMake(0, 0, 10, 0);
    // 3.锁定垂直或水平滚动(45度失效)
    scrollView.directionalLockEnabled = YES;
    // 4.在没有垂直或水平的contenOffsize也可以滚动,保证回弹(下拉刷新用到)
    scrollView.alwaysBounceVertical = YES;
    // 5.按页滚动
    scrollView.pagingEnabled = YES;
    // 6.指示器开启默认yes
    scrollView.showsHorizontalScrollIndicator = YES;
    // 7.指示器调初始的位置
    scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 100, 0, 0);
    // 8.指示器颜色
    scrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
    // 9.改变ContentOffset会有动画(点之间), scrollRectToVisible,(矩形区域)
//    - (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated;  // animate at constant velocity to new offset
//    - (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated;
    // 10.BOOL tracking (系统自动为NO,手动触摸触发为YES) 用户用手触摸但还没开始拖动,返回YES
    // 11.BOOL draging (判断是手拖动还是定时器走的) 用户开始拖动
    // 12.BOOL decelerating 拖动结束开始减速判定
    // 改变contenOffSize相当于scrollView的bounds改变,手势+bounds的修改就形成一个scrollView
    scrollView.delegate = self;
    scrollView.maximumZoomScale = 2;
    scrollView.minimumZoomScale = 1;
    [scrollView addSubview:_imageView];
    
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return self.imageView;
}
// 滚动就执行
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
   
    if (scrollView.dragging) {
        NSLog(@"拖动滚动");
    } else {
        NSLog(@"减速滚动");
    }
}
// 将要开始,此方法是拖拽之前.
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    NSLog(@"开始拖拽");
}
// 结束拖拽,将要减速
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    NSLog(@"结束拖拽,将要减速");
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    NSLog(@"开始减速");
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSLog(@"结束减速");
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    NSLog(@"已停止滚动");//没有触发
}
// 已经结束
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
